#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int mpt[110][110], dp[110][110],m, n;
int Solve(int i, int j)
{
	if (dp[i][j] == -1)
	{
		int dp1 = 0,dp2 = 0, dp3 = 0, dp4 = 0;
		if (i - 1 >= 0&&mpt[i][j]>mpt[i-1][j])dp1=dp[i-1][j] = Solve(i - 1, j);
		if (j - 1 >= 0 && mpt[i][j]>mpt[i][j-1])dp2=dp[i][j-1] = Solve(i, j - 1);
		if (i + 1 < n&&mpt[i][j]>mpt[i+1][j])dp3=dp[i + 1][j] = Solve(i + 1, j);
		if (j + 1 < m&&mpt[i][j]>mpt[i][j+1])dp4=dp[i][j+1] = Solve(i, j + 1);
		dp1 = dp1 > dp2 ? dp1 : dp2;
		dp3 = dp3 > dp4 ? dp3 : dp4;
		dp1 = dp1 > dp3 ? dp1 : dp3;
		dp[i][j] = dp1 +1;
	}
	return dp[i][j];
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n >> m;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
			{
				cin >> mpt[i][j];
				dp[i][j] = -1;
			}
			
		int ans = 0;
		for(int i=0;i<n;i++)
			for (int j = 0; j < m; j++)
			{
				int temp = Solve(i, j);
				ans = ans > temp ? ans : temp;
			}
		cout << ans << endl;
	}
	return 0;
}